/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.uch.biblioteca.hibernateSpringDao;
import java.util.List;
import java.sql.SQLException;
import java.util.Date;
import net.uch.biblioteca.mapping.BibPrestamo;
import org.hibernate.Criteria;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.hibernate.criterion.Restrictions;

public class HSBibPrestamoDAOImpl extends HibernateDaoSupport implements HSBibPrestamoDAO {
    
    public void guardarPrestamo(BibPrestamo prestamo) throws Exception{
        this.getHibernateTemplate().save( prestamo );
    }
    
    public List<BibPrestamo> traerReservasxLector ( int LecId ){
        
        List<BibPrestamo> lstReservas;
        
        lstReservas = this.getSession().createCriteria( BibPrestamo.class ).
                add( Restrictions.eq( "bibLector.lecId", LecId )).
                add(Restrictions.eq( "opeTip", "001003") ).
                add(Restrictions.eq( "preEst", "1") ).
                list();
        
        return lstReservas;
    }
    
    public List<BibPrestamo> traerPrestamosxLector ( int LecId ){
        
        List<BibPrestamo> lstReservas;
        
        lstReservas = this.getSession().createCriteria( BibPrestamo.class ).
                add( Restrictions.eq( "bibLector.lecId", LecId )).
                add(Restrictions.eq( "opeTip", "001001") ).
                add(Restrictions.eq( "preEst", "1") ).
                list();
        
        return lstReservas;
    }
    
    public List<BibPrestamo> traerHistorialxLector ( int LecId ){
        
        List<BibPrestamo> lstReservas;
        
        lstReservas = this.getSession().createCriteria( BibPrestamo.class ).
                add( Restrictions.eq( "bibLector.lecId", LecId )).
                list();
        
        return lstReservas;
    }
    
    @Override
    public void AprobarPrestamo ( int idPre, int usuId, String obsPre ) throws Exception {
        String sHqlUpdate;

        sHqlUpdate = "update BibPrestamo set usuId = :usuId, opeTip=:opeTip, preObs=:preObs where preId = :id";
        this.getSession().createQuery( sHqlUpdate ).setInteger( "usuId", usuId ).
                setString( "opeTip", "001001" ).
                setString( "preObs", obsPre ).
                setInteger( "id", idPre ).
                executeUpdate();
    }
    
    @Override
    public void cancelarReserva( int resId ) throws Exception{
        String sHqlUpdate;

        sHqlUpdate = "update BibPrestamo set preEst = :activo where preId = :id";
        this.getSession().createQuery( sHqlUpdate ).setString( "activo", "0" ).
                setInteger( "id", resId ).
                executeUpdate();
    }
    
    @Override
    public void devolverPrestamo( int resId, Date fecDdev ) throws Exception{
        String sHqlUpdate;

        sHqlUpdate = "update BibPrestamo set preEst = :activo, preFecDev =:fecDdev where preId = :id";
        this.getSession().createQuery( sHqlUpdate ).setString( "activo", "0" ).
                setInteger( "id", resId ).
                setDate("fecDdev", fecDdev).
                executeUpdate();
    }
    
    @Override
    public BibPrestamo buscarPrestamoPorId( Integer preId ) throws DataAccessException {
        return (BibPrestamo) this.getSession().get( BibPrestamo.class, preId );
    }
}
